package com.springboot.manager.config.interceptor;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @version v1.0
 * @ProjectName: hlk-microservice-ails
 * @ClassName: InterceptorConfig
 * @Description: TODO(一句话描述该类的功能)
 * @Author: zhangzl
 * @Date: 2021-07-30 15:22
 */
@Configuration
public class InterceptorConfig implements WebMvcConfigurer {

    /**
     * **************注解******************
     * 由于TokenInterceptor中Autowired了redisTemplate,
     * 但是拦截器在SpringContext初始化之前就执行了，Bean初始化之前它就执行了，所以它肯定是无法获取SpringIOC容器中的内容的。
     * 此时Autowired了redisTemplate为空！！！！！
     * 解决:
     * 我们让拦截器执行的时候实例化拦截器Bean，在拦截器配置类里面先实例化拦截器，这样就解决了问题
     */
    @Bean
    public TokenInterceptor getTokenInterceptor() {
        return new TokenInterceptor();
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(getTokenInterceptor()).addPathPatterns("/**").
                excludePathPatterns("/error", "/doc/**/", "/favicon.ico", "/user/add","/user/getUserByAccountAndPwd","/login/**/","/logOperate/add","/logException/add");
    }
}
